package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"my-zero-tiktok/common/xerr"

	"my-zero-tiktok/apps/comment/pb/pb"
	"my-zero-tiktok/apps/comment/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetCommentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentListLogic {
	return &GetCommentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetCommentList 视频评论列表
func (l *GetCommentListLogic) GetCommentList(in *pb.ListRequest) (*pb.ListResponse, error) {
	comments, err := l.svcCtx.CommentModel.FindList(l.ctx, in.VideoId)
	if err != nil {
		return nil, xerr.New(xerr.CommentGetListError)
	}

	list := make([]*pb.Comment, len(comments))
	for i, com := range comments {
		var comment pb.Comment
		_ = copier.Copy(&comment, &com)
		list[i] = &comment
	}
	return &pb.ListResponse{List: list}, nil
}
